<?php
class Alumno extends CI_Controller{
    private $_userInfo;
    private $_currentGroups;
    private $_currentFiles;
    
    private $_userCue;
    private $_courseSelected;
    
    function __construct() {
        parent::__construct();
  
        $this->load->helper(array('form', 'url'));
        // load Models
        $this->load->model('BlockModel');
        $this->load->model('FileModel');
        
        $this->_userInfo = $this->session->userData('userInformation');
        $this->_currentGroups = $this->session->userData('currentGroups');
        
    }
    
    function index() {
        $this->_userCue = $this->session->userData('userCue');
        $this->_courseSelected = $this->session->userData('courseSelected');
        
        if ($this->_userCue) {
            $data['userCue'] = $this->_userCue;
            $this->load->view('FileUploadView', $data);
        }else{
            $this->_currentGroups = $this->BlockModel->getGroupByState($this->_userInfo->userId, 'habilitado');
            if (count($this->_currentGroups) == 1)  {
                $this->session->set_userdata('currentGroups',  $this->_currentGroups);
                $this->loadView('FileUploadView');
            }else{
                $this->loadView('DisabledUploadView');
            } 
       }
        
        
    }
    
    function loadView($viewName){
        if ($viewName == 'FileUploadView'){
            $data['userInfo'] = $this->_userInfo;
            $data['files'] = $this->getFiles();
            $this->load->view($viewName,$data);
        }else if ($viewName == 'DisabledUploadView'){
            $this->load->view($viewName);
        }
    }
    
    function reloadDisabledPage() {
        redirect('http://localhost/FileCollector/Alumno');
    }
    
    function getFiles(){
        $this->_currentFiles = $this->FileModel->getFilesByUserAndGroup($this->_userInfo->userId, $this->_currentGroups[0]->groupId);
        return $this->_currentFiles;
    }
    
    
    function upload(){
        $this->_userCue = $this->session->userData('userCue');
            $this->_courseSelected = $this->session->userData('courseSelected');
            
        if ($this->_userCue) {
            $examType = 'parcial';
            $course = $this->_courseSelected;
            $cue = $this->_userCue;
            $fileInfo = $_FILES["selectedFile"];
            
            $realPath = "tempUploads/".$examType."/".$course."/".$cue;
            
            mkdir(utf8_decode($realPath), 0777, true);
            
            $config['upload_path'] = "./".utf8_decode($realPath);
            $config['allowed_types'] = '*';
            $config['max_size']	= '900000';
        
            $this->upload->initialize($config);
        
            if ( ! $this->upload->do_upload("selectedFile")) {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            }else{
                $data = array('upload_data' => $this->upload->data());
                $this->load->view('upload_success', $data);
            }
        
            $this->saveLogForSpecialExam($examType, $course, $cue, $fileInfo);
            
       
            
            
            
        }else{
            $course = $this->_currentGroups[0]->courseName;
            $block = $this->_currentGroups[0]->blockName;
            $userName = $this->_userInfo->userName;
            $fileInfo = $_FILES["selectedFile"];
            
            $realPath = "tempUploads/".$course."/".$block."/".$userName;
            
            mkdir(utf8_decode($realPath), 0777, true);
            
            $config['upload_path'] = "./".utf8_decode($realPath);
            $config['allowed_types'] = '*';
            $config['max_size']	= '900000';
        
            $this->upload->initialize($config);
        
            if ( ! $this->upload->do_upload("selectedFile")) {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            }else{
                $data = array('upload_data' => $this->upload->data());
                $this->load->view('upload_success', $data);
            }
        
            $this->saveLogForApplication($course, $block, $userName, $fileInfo);
            $this->saveLogForCourse($course, $block, $userName, $fileInfo);
            $this->saveLogForBlock($course, $block, $userName, $fileInfo);
            $this->saveLogForUser($course, $block, $userName, $fileInfo);
        
        
            // insertar a la tabla file
            $fileId = $this->FileModel->getNextAutoIncrementValue();
            $name = $fileInfo['name'];
            $size = $fileInfo['size'];
            $date = $fileInfo['name'];
            $ip = $_SERVER['REMOTE_ADDR'];
            $this->FileModel->addFile($name, $size, $date, $ip);
        
            $userId = $this->_userInfo->userId;
            $groupId = $this->_currentGroups[0]->groupId;
            // insertar a la tabla user_file
            $this->FileModel->addUserFile($userId, $fileId, $groupId);
        }
   }
    
    /* PARA EL LOG */
    
    function saveLogForApplication ($course, $block, $userName, $fileInfo){
        
        $realPath = "tempUploads";
        date_default_timezone_set("US/Eastern").
        $remote_address = $_SERVER['REMOTE_ADDR'];
        $log = "USERNAME: ".$userName.PHP_EOL.
               "CURSO: ".$course.PHP_EOL.
               "BLOQUE: ".$block.PHP_EOL.
               "FILE: ".$fileInfo['name'].PHP_EOL.
               "FILE SIZE: ".$fileInfo['size'].PHP_EOL.
               "FILE TYPE: ".$fileInfo['type'].PHP_EOL.
               "IP ADDRESS: ".$remote_address.PHP_EOL.
               "DATE: ".date("F j, Y, g:i:s a").PHP_EOL.
                "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".PHP_EOL;
                
        file_put_contents("./".utf8_decode($realPath)."/log_".date("j_n_Y").'.txt', $log, FILE_APPEND);
        

        // insertar a la tabla file
        // 
        // insertar a la tabla user_file
        
        
    }
    
    function saveLogForCourse($course, $block, $userName, $fileInfo){
        $realPath = "tempUploads/".$course;
        date_default_timezone_set("US/Eastern").
        
        $remote_address = $_SERVER['REMOTE_ADDR'];
        $log = "USERNAME: ".$userName.PHP_EOL.
               "BLOQUE: ".$block.PHP_EOL.
               "FILE: ".$fileInfo['name'].PHP_EOL.
               "FILE SIZE: ".$fileInfo['size'].PHP_EOL.
               "FILE TYPE: ".$fileInfo['type'].PHP_EOL.
               "IP ADDRESS: ".$remote_address.PHP_EOL.
               "DATE: ".date("F j, Y, g:i:s a").PHP_EOL.
                "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".PHP_EOL;         
        file_put_contents("./".utf8_decode($realPath)."/log_".date("j_n_Y").'.txt', $log, FILE_APPEND);
    }
    
    function saveLogForBlock($course, $block, $userName, $fileInfo){
        $realPath = "tempUploads/".$course."/".$block;
        date_default_timezone_set("US/Eastern").
        
        $remote_address = $_SERVER['REMOTE_ADDR'];
        $log = "USERNAME: ".$userName.PHP_EOL.
               "FILE: ".$fileInfo['name'].PHP_EOL.
               "FILE SIZE: ".$fileInfo['size'].PHP_EOL.
               "FILE TYPE: ".$fileInfo['type'].PHP_EOL.
               "IP ADDRESS: ".$remote_address.PHP_EOL.
               "DATE: ".date("F j, Y, g:i:s a").PHP_EOL.
                "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".PHP_EOL;
                
        file_put_contents("./".utf8_decode($realPath)."/log_".date("j_n_Y").'.txt', $log, FILE_APPEND);  
    }
    
    
    
    function saveLogForUser($course, $block, $userName, $fileInfo){
    $realPath = "tempUploads/".$course."/".$block."/".$userName;
        date_default_timezone_set("US/Eastern").
        $remote_address = $_SERVER['REMOTE_ADDR'];
        $log = "FILE: ".$fileInfo['name'].PHP_EOL.
               "FILE SIZE: ".$fileInfo['size'].PHP_EOL.
               "FILE TYPE: ".$fileInfo['type'].PHP_EOL.
               "IP ADDRESS: ".$remote_address.PHP_EOL.
               "DATE: ".date("F j, Y, g:i:s a").PHP_EOL.
                "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".PHP_EOL;
                
        file_put_contents("./".utf8_decode($realPath)."/log_".date("j_n_Y").'.txt', $log, FILE_APPEND);   
    }
    
    
    
    
    function saveLogForSpecialExam($examType, $course, $cue, $fileInfo){
        //$realPath = "tempUploads/".$examType."/".$course."/".$cue;
        $realPath = "tempUploads/".$examType."/".$course;
        date_default_timezone_set("US/Eastern").
        $remote_address = $_SERVER['REMOTE_ADDR'];
        $log = "CUE: ".$cue.PHP_EOL.
               "CURSO: ".$course.PHP_EOL.
               "EVALUACION: ".$examType.PHP_EOL.
               "FILE: ".$fileInfo['name'].PHP_EOL.
               "FILE SIZE: ".$fileInfo['size'].PHP_EOL.
               "FILE TYPE: ".$fileInfo['type'].PHP_EOL.
               "IP ADDRESS: ".$remote_address.PHP_EOL.
               "DATE: ".date("F j, Y, g:i:s a").PHP_EOL.
                "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ".PHP_EOL;
                
        file_put_contents("./".utf8_decode($realPath)."/log_".date("j_n_Y").'.txt', $log, FILE_APPEND); 
    }
    
}